package com.example.qxfw.nyqx.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.qxfw.nyqx.mapper.SumRainMonthDataMapper;
import com.example.qxfw.nyqx.mapper.SumSunMonthDataMapper;
import com.example.qxfw.nyqx.mapper.SumTempMonthDataMapper;
import com.example.qxfw.nyqx.service.FyqtjService;
import com.example.qxfw.nyqx.vo.SumDataVo;
import com.example.qxfw.nyqx.vo.SumRainAndSunAndTempListVo;
import com.example.qxfw.common.entity.ny.StationInfo;
import com.example.qxfw.common.entity.qx.SumRainMonthData;
import com.example.qxfw.common.entity.qx.SumSunMonthData;
import com.example.qxfw.common.entity.qx.SumTempMonthData;
import com.example.qxfw.rcyw.mapper.SmallScaleStationMapper;
import com.example.qxfw.rcyw.mapper.StationInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FyqtjServiceImpl implements FyqtjService {
    @Autowired
    private StationInfoMapper stationInfoMapper;

    @Autowired
    private SmallScaleStationMapper smallScaleStationMapper;

    @Autowired
    private SumRainMonthDataMapper sumRainMonthDataMapper;

    @Autowired
    private SumSunMonthDataMapper sumSunMonthDataMapper;

    @Autowired
    private SumTempMonthDataMapper sumTempMonthDataMapper;

    @Override
    public SumRainAndSunAndTempListVo getSumRainAndSunAndTempList(String stationId, Integer year1, Integer month1, Integer year2, Integer month2) {
        StationInfo stationInfo = stationInfoMapper.selectById(stationId);
        String smallScaleStationId = stationInfo.getSmallscalestationid();
        List<SumTempMonthData> sumTempMonthDataList = new ArrayList<>();
        List<SumSunMonthData> sumSunMonthDataList = new ArrayList<>();
        List<SumRainMonthData> sumRainMonthDataList = new ArrayList<>();
        if (year1.equals(year2)) {
            sumTempMonthDataList = sumTempMonthDataMapper.selectList(Wrappers.<SumTempMonthData>lambdaQuery()
                    .eq(SumTempMonthData::getStationid, smallScaleStationId)
                    .eq(SumTempMonthData::getSumindicatorid, "平均气温")
                    .ge(SumTempMonthData::getMonth, month1)
                    .le(SumTempMonthData::getMonth, month2)
                    .orderByAsc(SumTempMonthData::getYear)
                    .orderByAsc(SumTempMonthData::getMonth)
            );
            sumSunMonthDataList = sumSunMonthDataMapper.selectList(Wrappers.<SumSunMonthData>lambdaQuery()
                    .eq(SumSunMonthData::getStationid, smallScaleStationId)
                    .eq(SumSunMonthData::getSumindicatorid, "日照时数")
                    .ge(SumSunMonthData::getMonth, month1)
                    .le(SumSunMonthData::getMonth, month2));
            sumRainMonthDataList = sumRainMonthDataMapper.selectList(Wrappers.<SumRainMonthData>lambdaQuery()
                    .eq(SumRainMonthData::getStationid, smallScaleStationId)
                    .eq(SumRainMonthData::getSumindicatorid, "总雨量")
                    .ge(SumRainMonthData::getMonth, month1)
                    .le(SumRainMonthData::getMonth, month2));
        } else {
            sumTempMonthDataList = sumTempMonthDataMapper.selectList(Wrappers.<SumTempMonthData>lambdaQuery()
                    .eq(SumTempMonthData::getStationid, smallScaleStationId)
                    .eq(SumTempMonthData::getSumindicatorid, "平均气温")
                    .and(i -> i
                            .eq(SumTempMonthData::getYear, year1).ge(SumTempMonthData::getMonth, month1)
                            .or()
                            .gt(SumTempMonthData::getYear, year1).lt(SumTempMonthData::getYear, year2)
                            .or()
                            .eq(SumTempMonthData::getYear, year2).le(SumTempMonthData::getMonth, month2))
                    .orderByAsc(SumTempMonthData::getYear)
                    .orderByAsc(SumTempMonthData::getMonth));
            sumSunMonthDataList = sumSunMonthDataMapper.selectList(Wrappers.<SumSunMonthData>lambdaQuery()
                    .eq(SumSunMonthData::getStationid, smallScaleStationId)
                    .eq(SumSunMonthData::getSumindicatorid, "日照时数")
                    .and(i -> i
                            .eq(SumSunMonthData::getYear, year1).ge(SumSunMonthData::getMonth, month1)
                            .or()
                            .gt(SumSunMonthData::getYear, year1).lt(SumSunMonthData::getYear, year2)
                            .or()
                            .eq(SumSunMonthData::getYear, year2).le(SumSunMonthData::getMonth, month2)));
            sumRainMonthDataList = sumRainMonthDataMapper.selectList(Wrappers.<SumRainMonthData>lambdaQuery()
                    .eq(SumRainMonthData::getStationid, smallScaleStationId)
                    .eq(SumRainMonthData::getSumindicatorid, "总雨量")
                    .and(i -> i
                            .eq(SumRainMonthData::getYear, year1).ge(SumRainMonthData::getMonth, month1)
                            .or()
                            .gt(SumRainMonthData::getYear, year1).lt(SumRainMonthData::getYear, year2)
                            .or()
                            .eq(SumRainMonthData::getYear, year2).le(SumRainMonthData::getMonth, month2)));
        }
        SumRainAndSunAndTempListVo sumRainAndSunAndTempListVo = new SumRainAndSunAndTempListVo();
        List<SumDataVo> tempDataList = new ArrayList<>();
        List<SumDataVo> sunDataList = new ArrayList<>();
        List<SumDataVo> rainDataList = new ArrayList<>();
        for (SumTempMonthData sumTempMonthData : sumTempMonthDataList) {
            String time = sumTempMonthData.getYear() + "-" + sumTempMonthData.getMonth();
            SumDataVo tempData = new SumDataVo(time, sumTempMonthData.getSumvalue());
            tempDataList.add(tempData);
            SumDataVo sunData = new SumDataVo(time,0.0);
            for (SumSunMonthData sumSunMonthData : sumSunMonthDataList) {
                if (sumSunMonthData.getYear() == sumTempMonthData.getYear() &&
                        sumSunMonthData.getMonth() == sumTempMonthData.getMonth()) {
                    sunData = new SumDataVo(time, sumSunMonthData.getSumvalue());
                    break;
                }
            }
            sunDataList.add(sunData);
            SumDataVo rainData = new SumDataVo(time,0.0);
            for (SumRainMonthData sumRainMonthData : sumRainMonthDataList) {
                if (sumRainMonthData.getYear() == sumTempMonthData.getYear() &&
                        sumRainMonthData.getMonth() == sumTempMonthData.getMonth()) {
                    rainData = new SumDataVo(time, sumRainMonthData.getSumvalue());
                    break;
                }
            }
            rainDataList.add(rainData);
        }
        sumRainAndSunAndTempListVo.setTempData(tempDataList);
        sumRainAndSunAndTempListVo.setSunData(sunDataList);
        sumRainAndSunAndTempListVo.setRainData(rainDataList);

        return sumRainAndSunAndTempListVo;
    }
}
